import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { getToken } from '@/utils/request'

// 布局组件
import MainLayout from '@/layouts/MainLayout.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: MainLayout,
      children: [
        {
          path: '',
          name: 'home',
          component: () => import('@/views/Home.vue'),
          meta: { title: '首页' }
        },
        {
          path: 'culture',
          name: 'culture',
          component: () => import('@/views/culture/CultureList.vue'),
          meta: { title: '剪纸文化' }
        },
        {
          path: 'culture/:id',
          name: 'culture-detail',
          component: () => import('@/views/culture/CultureDetail.vue'),
          meta: { title: '文化详情' }
        },
        {
          path: 'tutorial',
          name: 'tutorial',
          component: () => import('@/views/tutorial/TutorialList.vue'),
          meta: { title: '教程学习' }
        },
        {
          path: 'tutorial/:id',
          name: 'tutorial-detail',
          component: () => import('@/views/tutorial/TutorialDetail.vue'),
          meta: { title: '教程详情' }
        },
        {
          path: 'tutorial/:id/study',
          name: 'tutorial-study',
          component: () => import('@/views/tutorial/TutorialStudy.vue'),
          meta: { title: '学习教程', requiresAuth: true }
        },
        {
          path: 'news',
          name: 'news',
          component: () => import('@/views/news/NewsList.vue'),
          meta: { title: '活动资讯' }
        },
        {
          path: 'news/:id',
          name: 'news-detail',
          component: () => import('@/views/news/NewsDetail.vue'),
          meta: { title: '资讯详情' }
        },
        {
          path: 'search',
          name: 'search',
          component: () => import('@/views/Search.vue'),
          meta: { title: '搜索' }
        },
        {
          path: 'ai-assistant',
          name: 'ai-assistant',
          component: () => import('@/views/ai/AIAssistant.vue'),
          meta: { title: 'AI助手' }
        },
        // 用户相关页面（需要登录）
        {
          path: 'profile',
          name: 'profile',
          component: () => import('@/views/user/Profile.vue'),
          meta: { title: '个人中心', requiresAuth: true }
        },
        {
          path: 'profile/edit',
          name: 'profile-edit',
          component: () => import('@/views/user/ProfileEdit.vue'),
          meta: { title: '编辑资料', requiresAuth: true }
        },
        {
          path: 'favorites',
          name: 'favorites',
          component: () => import('@/views/user/Favorites.vue'),
          meta: { title: '我的收藏', requiresAuth: true }
        },
        {
          path: 'study-records',
          name: 'study-records',
          component: () => import('@/views/user/StudyRecords.vue'),
          meta: { title: '学习记录', requiresAuth: true }
        },
        {
          path: 'settings',
          name: 'settings',
          component: () => import('@/views/user/Settings.vue'),
          meta: { title: '设置', requiresAuth: true }
        }
      ]
    },
    // 认证页面（不使用主布局）
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/auth/Login.vue'),
      meta: { title: '登录', hideForAuth: true }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/auth/Register.vue'),
      meta: { title: '注册', hideForAuth: true }
    },
    // 404页面
    {
      path: '/:pathMatch(.*)*',
      name: 'not-found',
      component: () => import('@/views/NotFound.vue'),
      meta: { title: '页面未找到' }
    }
  ],
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const token = getToken()

  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 剪纸艺术平台` : '剪纸艺术平台'

  // 检查是否需要登录
  if (to.meta.requiresAuth) {
    if (!token) {
      // 未登录，跳转到登录页
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
      return
    }
  }

  // 如果已登录，不允许访问登录/注册页
  if (to.meta.hideForAuth && token) {
    next({ path: '/' })
    return
  }

  next()
})

export default router
